package org.ndx.jsg.output.freemarker;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.ndx.jsg.FileInfos;

public class DefaultProcessableFiles implements ProcessableFiles {
	/**
	 * Directory associated to root output folder
	 */
	private Directory root = new Directory("", "", "");

	@Override
	public void add(FileInfos infos) {
		String relativePath = infos.getRelativePath();
		String[] exploded = Utils.split(relativePath);
		List<String> coordinates = new LinkedList<String>(Arrays.asList(exploded));
		// Last element is file name, which is irrelevant here
		coordinates.remove(coordinates.size()-1);
		root.addAt(coordinates, infos);
	}

	/**
	 * Process all contained files using given set of templates
	 */
	@Override
	public void process(TemplateProvider provider) {
		TemplateApplier templateAppier = new TemplateApplier(provider);
		root.setResolver(templateAppier);
		root.accept(templateAppier);
	}

}
